﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Text;

namespace smartphone.UserControls
{
    public partial class AdminComment : System.Web.UI.UserControl
    {
        private Comment com = new Comment();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            GridViewComment.DataSource = com.GetAllComment();
            GridViewComment.DataBind();
        }

        /// <summary>
        /// Hien thi messagebox thong bao loi
        /// </summary>
        /// <param name="msg">Nội dung thông báo được đưa ra</param>
        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void GridViewComment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridViewComment.DataKeys[e.RowIndex].Value.ToString();

            bool success = com.DeleteComment(Int32.Parse(id));
            //LabelInform.Text = success ? "Xóa thành công" : "Xóa thất bại";
            BindGrid();
            ShowPopUpMsg("Xóa bình luận thành công.");
        }

    }
}